package cn.edu.zafu.bigdata.zyf;

// 定义自定义异常类InvalidAgeException，继承自Exception
class InvalidAgeException extends Exception {
    public InvalidAgeException(String message) {
        super(message);
    }
}

public class CustomExceptionHandling {

    // 方法checkAge，如果年龄小于18则抛出InvalidAgeException
    public static void checkAge(int age) throws InvalidAgeException {
        if (age < 18) {
            throw new InvalidAgeException("Age must be at least 18. Invalid age provided: " + age);
        }
        System.out.println("Age is valid: " + age);
    }

    public static void main(String[] args) {
        int age = 17; // 测试年龄小于18的情况

        try {
            // 尝试调用checkAge方法
            checkAge(age);
        } catch (InvalidAgeException e) {
            // 捕获异常并输出提示信息
            System.out.println("Error: " + e.getMessage());
        }
    }
}